const { execSQL } = require('../../db/mysql');

//获取全部订单列表方法
function getList() {
    const sql = 'select * from \`order\`';
    return execSQL(sql);
}

//前台获取订单列表方法
const getListById = (orderData = {}) => {
    const userName = orderData.user_name;
    const sql = `select * from \`order\` where user_name='${userName}'`;
    return execSQL(sql);
}


//获取详情数据方法
const getDetail = id => {
    const sql = `select * from \`order\` where order_id='${id}'`;
    return execSQL(sql);
}

//添加新订单
const createNewOrder = (orderData = {}) => {
    const productInfo = orderData.pdt_info;
    const orderPrice = orderData.order_price;
    const userName = orderData.user_name;
    const orderTime = orderData.order_time;
    const userAddr = orderData.user_addr;
    const orderState = orderData.order_state;
    const userPhone = orderData.user_phone;
    const orderNumber = orderData.order_number;

    const sql = `insert into \`order\` (order_number, pdt_info, order_price, user_name, user_addr, user_phone, order_time, order_state) values ('${orderNumber}', '${productInfo}', ${orderPrice}, '${userName}', '${userAddr}', '${userPhone}', '${orderTime}', '${orderState}')`;
    return execSQL(sql).then(insertedResult => {
        return {
            id: insertedResult.insertId
        }
    });
}


//删除订单
const deleteOrder = id => {
    const sql = `delete from \`order\` where order_id=${id}`
    return execSQL(sql).then(deleteResult => {
        if (deleteResult.affectedRows > 0) {
            return true;
        }
        return false;
    })
}

const changeOrder = (id, orderData = {}) => {
    const orderState = orderData.order_state;
    const sql = `update \`order\` set order_state=${orderState} where order_id=${id}`;
    return execSQL(sql).then(updateResult => {
        if (updateResult.affectedRows > 0) {
            return true;
        }
        return false;
    });
}
//导出方法
module.exports = { 
    getList,
    getListById,
    getDetail,
    createNewOrder,
    deleteOrder,
    changeOrder,
  
}

